﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ToolGetData.Entities;
using ToolGetData.Enums;

namespace ToolGetData.Data_Access_Layer
{
    class RingtoneDAL
    {
        private DataAccessHelper db;

        public RingtoneDAL()
        {
            db = new DataAccessHelper();
        }

        public bool InsertRingtone(Ringtone ringtone)
        {
            int count = 0;
            if (!CheckRingtoneCode(ringtone.Code))
            {
                count = db.ExecuteNonQuery(@"Insert tblRingtone (Code,Title,Artist,Url,Download,Size) Values ('" + ringtone.Code + "','" + ringtone.Title + "','" + ringtone.Artist + "','" + ringtone.Url.AbsoluteUri + "','" + ringtone.Download + "','" + ringtone.Size + "')");
                if (count > 0)
                {
                    count = db.ExecuteNonQuery(@"Insert tblCategory_Ringtone (RingtoneCode, CategoryId) Values ('" + ringtone.Code + "','" + ringtone.CateId + "')");
                }
            }
            else 
            {
                if (!CheckRingtoneCode(ringtone.Code,ringtone.CateId))
                {
                    count = db.ExecuteNonQuery(@"Insert tblCategory_Ringtone (RingtoneCode, CategoryId) Values ('" + ringtone.Code + "','" + ringtone.CateId + "')");
                }
                
            }
            return count > 0;
        }


        public int InsertRingtones(Stack<Ringtone> ringtoneStack)
        {
            int count = 0;
            foreach (Ringtone ringtone in ringtoneStack)
            {
                if (InsertRingtone(ringtone))
                {
                    count++;
                }
            }
            return count;
        }

        /// <summary>
        /// Kiểm tra xem ringtone code đã có trong bảng tblRingtone chưa
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public bool CheckRingtoneCode(long code)
        {
            int count = (int)db.ExecuteScalar("Select count(Code) From tblRingtone Where Code = '" + code + "'");
            return count > 0;
        }

        /// <summary>
        /// Kiểm tra xem ringtone code đã có trong bảng tblCategory_Ringtone chưa
        /// </summary>
        /// <param name="code"></param>
        /// <param name="cateId"></param>
        /// <returns></returns>
        public bool CheckRingtoneCode(long code, int cateId)
        {
            int count = (int)db.ExecuteScalar("Select count(RingtoneCode) From tblCategory_Ringtone Where RingtoneCode = '" + code + "' and CategoryId = '" + cateId + "'");
            return count > 0;
        }

        public List<string> GetAllCategory()
        {
            using (SqlDataReader reader = db.ExecuteReader("Select Category From tblCategory"))
            {
                List<string> categoryList = new List<string>();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        string category = reader["Category"].ToString();
                        categoryList.Add(category);
                    }
                }
                return categoryList;
            }
        }
    }
}
